//
//  SZLSingleListViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/15.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLSingleListViewController.h"
#import "SingleListTableViewCell.h"
#import "SingleListNetWork.h"
#import "SinglePayNetwork.h"
#import "SZLAccountHelper.h"
#import "SingleListModel.h"
#import "MBProgressHUD+MJ.h"
#import <MJRefresh.h>
#import "SZLSingleDetailViewController.h"
#import "ConGoodsDetailController.h"
#import "ConSingleDetailController.h"
#import "UIViewController+RTRootNavigationController.h"
#import "SingleActionTool.h"
#import "SZLSinglePayViewController.h"

@interface SZLSingleListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSMutableArray *sources;
@property (nonatomic,strong) UITableView *sourceTab;
@property (nonatomic,assign) NSInteger curPage;

@end

@implementation SZLSingleListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.curPage = 1;
    [self setView];
    [self.sourceTab.mj_header beginRefreshing];
}
- (void)setView
{
    if (self.type == SingleTypeUnPay) {
        self.title = @"待付款";
    }else if (self.type == SingleTypeReadyDeliver) {
        self.title = @"待发货";
    }else if (self.type == SingleTypeReadyReceive) {
        self.title = @"待收货";
    }else if (self.type == SingleTypeComplete) {
        self.title = @"已完成";
    }else {
        self.title = @"全部订单";
    }
    if (self.isFromPay) {
        self.rt_disableInteractivePop = YES;
    }
    [self.view addSubview:self.sourceTab];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SingleListModel *model = self.sources[indexPath.section];
    if (model.status>1) {
        return 183;
    }
    return 233;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sources.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SingleListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SingleListTableViewCell class])];
    cell.model = self.sources[indexPath.section];
    cell.clipsToBounds = YES;
    __WEAK
    cell.detailBlock = ^(SingleListModel *model) {
        ConGoodsDetailController *detailVc = [[ConGoodsDetailController alloc]init];
        detailVc.productNo = model.productNo;
        [weakSelf.navigationController pushViewController:detailVc animated:YES];
    };
    cell.payBlock = ^(SingleListModel *model) {
        [weakSelf gotoPayWithModel:model];
    };
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ConSingleDetailController *detailVc = [[ConSingleDetailController alloc]init];
    detailVc.listModel = self.sources[indexPath.section];
    __WEAK
    detailVc.successBlock = ^{
        self.curPage = 1;
        [weakSelf getSingleList];
    };
    [self.navigationController pushViewController:detailVc animated:YES];
}
- (UITableView *)sourceTab
{
    if (!_sourceTab) {
        _sourceTab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64) style:UITableViewStyleGrouped];
        _sourceTab.delegate = self;
        _sourceTab.dataSource = self;
        _sourceTab.sectionFooterHeight = 0.01;
        _sourceTab.tableFooterView = [[UIView alloc]init];
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([SingleListTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([SingleListTableViewCell class])];
        _sourceTab.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.curPage = 1;
            [self getSingleList];
        }];
        _sourceTab.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.curPage++;
            [self getSingleList];
        }];
        _sourceTab.mj_footer.hidden = YES;
    }
    return _sourceTab;
}
- (void)gotoPayWithModel:(SingleListModel *)model
{
    __WEAK
    [[SingleActionTool shareTool] singleToPayWithModel:model success:^(SingleCreateModel *model) {
        SZLSinglePayViewController *payVc = [[SZLSinglePayViewController alloc]init];
        payVc.model = model;
        payVc.payChargeType = PayChargeTypeSingle;
        payVc.paySuccessBlock = ^(){
        };
        [weakSelf.navigationController pushViewController:payVc animated:YES];
    }];
}
//付款成功后获取指定payNumber的订单列表
- (void)lookPayNumberSuccessList
{
    self.sourceTab.mj_footer.hidden = YES;
    self.sourceTab.mj_header.hidden = YES;
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"payNumber"] = self.model.payNumber;
    [self showWaiter];
    [SinglePayNetwork lookSuccessPayListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        if (success) {
            [self.sources addObjectsFromArray:lists];
            [self.sourceTab reloadData];
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
//获取订单列表
- (void)getSingleList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    if (self.type) {
        parame[@"status"] = @(self.type);
    }
    parame[@"pageNo"] = @(self.curPage);
    [self showClearBgWaiter];
    //免费喝红酒 普通订单 商家订单
    [SingleListNetWork getSingleListWithParame:parame type:self.type response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        [self.sourceTab.mj_header endRefreshing];
        [self.sourceTab.mj_footer endRefreshing];
        if (success) {
            [self hideNoDataView];
            self.sourceTab.mj_footer.hidden = lists.count<20;
            if (self.curPage == 1) {
                [self.sources removeAllObjects];
                if (lists.count == 0) {
                    [self showNoDataView:CGRectMake(0, 0, ScreenW, ScreenH) title:@"没有符合条件的商品"];
                    return ;
                }
            }
            [self.sources addObjectsFromArray:lists];
            [self.sourceTab reloadData];
        }else{
            if ([msg isEqualToString:NoNetworkStatus]) {
                [self showRequestAgainView:@selector(getSingleList)];
            }
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}

@end
